import inputCommon from '../../../../mixin/inputCommon'
import requiredLabel from '../../../../components/requiredLabel'

export default {
  name: 'render',
  components: { requiredLabel },
  props: ['config', 'editor', 'isMobile'],
  inject: ['rootForm'],
  mixins: [inputCommon],
  data() {
    return {
      imgSrc: undefined,
      clickSYS: () => {
        this.scan()
      },
      noData: "{\n" +
      "    \"app_key\":\"三方H5APP唯一识别号\",\n" +
      "    \"version\":\"1.0.0\"\n" +
      "}\n"
    }
  },
  methods: {
    scan() {

      dsBridge.call("FunctionApi.openCameraPicture", this.noData, (v) => {
        this.upfile(JSON.parse(v).data)
      })
    },
    upfile(data) {
      if(data.length / 1000 > 20000) {
        return $message.warning('文件太大，照片不能大于20M')
      }
      dsBridge.call("UIApi.showLoading", JSON.stringify(this.noData), function(v) {
        console.log(v)
      });
      var noData = {
        app_key: 'UNICLOUD_APP',
        version: 'V1.0.0',
        data: {
          upload: {
            url: window.env.uploadUrl,
            fileKey: 'file',
            filePath: data.path,
            formParam: {
              fileName: 'pic' + (new Date()).getTime(),
              discription: '',
              tagIds: '',
              relationFlag: true
            },
            isLoading: 'true'
          }
        }
      }
      this.rootForm.properties[this.config.propertyName] = data
      dsBridge.call("FunctionApi.uploadFile", JSON.stringify(noData), (res) => {
        let resolve = JSON.parse(res)
        if (resolve.data && resolve.data.state === 0) {
          var imgUrl = ''
          if (resolve.data.result) {
            imgUrl = JSON.parse(resolve.data.result).data.filePath
          }
          this.rootForm.properties[this.config.propertyName] = imgUrl
          dsBridge.call("UIApi.hideLoading", JSON.stringify(this.noData), function(v) {
            console.log(v)
          })
        }else if(resolve.data && resolve.data.state === 1) {

        }else if(resolve.data && resolve.data.state === -1) {
          $message.error('上传文件失败')
        }else{
          $message.error('上传文件错误')
        }
      })
    }
  }
}
